﻿using System;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using ContractManagementSystem.BusinessLogic;
using ContractManagementSystem.Entity;

namespace ContractManagementSystem
{
    public partial class ViewMessage : System.Web.UI.Page
    {
        private static int _type = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                FetchAll();
                ViewMessagePanel.Visible = true;
                NewMessagePanel.Visible = false;
            }
        }

        private void FetchAll()
        {
            var table = MessageBusiness.FetchAll(Session["Name"].ToString(), _type);
            MessageListView.DataSource = table;
            MessageListView.DataBind();
            PagePanel.Visible = table.Rows.Count>0;
        }

        protected void DataPagerListPage(object sender, EventArgs e)
        {
            FetchAll();
        }


        protected void EditOnCommand(object sender, CommandEventArgs commandEventArgs)
        {
           
        }

        protected void NewListViewItemEditing(object sender, EventArgs e)
        {
        }

        protected void NewListViewItemDeleting(object sender, EventArgs e)
        {
        }

        protected void InboxLinkButton_OnClick(object sender, EventArgs e)
        {
            _type = 1;
            FetchAll();
            ViewMessagePanel.Visible = true;
            NewMessagePanel.Visible = false;
        }

        protected void SendItemsLinkButton_OnClick(object sender, EventArgs e)
        {
            _type = 2;
            FetchAll();
            ViewMessagePanel.Visible = true;
            NewMessagePanel.Visible = false;
        }

        protected void NewMessageLinkButton_OnClick(object sender, EventArgs e)
        {
            ViewMessagePanel.Visible = false;
            NewMessagePanel.Visible = true;
        }

        protected void DeleteLinkButton_OnClick(object sender, EventArgs e)
        {
            var table = new DataTable{Locale = CultureInfo.CurrentCulture};
            table.Columns.Add("MessageId", typeof (Guid));
            foreach (var item in MessageListView.Items)
            {
                var checkBox = item.FindControl("ItemCheckbox") as HtmlInputCheckBox;
                if(checkBox!=null && checkBox.Checked)
                {
                    table.Rows.Add(checkBox.Value);
                }
            }
            MessageBusiness.Delete(table);
        }

        protected void SendButtonClick(object sender, EventArgs e)
        {
            var fromUser = Session["Name"].ToString();
            var toUser = UserTextBox.Text;
            var content = ContentTextBox.Text;

            if (UserBusiness.CheckContain(UserTextBox.Text))
            {
                var entity = new MessageEntity
                {
                    FromUserId = fromUser,
                    ToUserId = toUser,
                    Content = content
                };

                if (MessageBusiness.Insert(entity))
                {
                    MessLabel.ForeColor = Color.Teal;
                    MessLabel.Text = "Success !";
                }
                else
                {
                    MessLabel.ForeColor = Color.Red;
                    MessLabel.Text = "Failure !";
                }
            }
            else
            {
                CheckImage.ImageUrl = "../Images/agt_action_fail1.png";
                MessLabel.ForeColor = Color.Red;
                MessLabel.Text = "Failure !";
            }
        }

        protected void ClearButtonClick(object sender, EventArgs e)
        {
            UserTextBox.Text = String.Empty;
            ContentTextBox.Text = String.Empty;
        }

        protected void CheckNameLinkButtonClick(object sender, EventArgs e)
        {
            CheckImage.ImageUrl = UserBusiness.CheckContain(UserTextBox.Text) ?
                "../Images/tools_check_spelling.png" : "../Images/agt_action_fail1.png";
        }
    }
}
